package db;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Table {

	protected static class Column{
		private String column;

		public Column(String name, String type){
			column = "\""+name+"\" "+type;
		}
		
		public Column(String name, String type, String properties) {
			this(name,type);
			column += " "+properties;
		}

		@Override
		public String toString() {
			return column;
		}
	}
	
	protected static boolean existsEntry(String table, String column, String value){
		boolean result = false;
		try {
			PreparedStatement statement = Database.getPreparedStatement("SELECT COUNT(\""+column+"\") FROM \""+table+"\" WHERE \""+column+"\"=?;");
			statement.setString(1, value);
			ResultSet resultSet = statement.executeQuery();
			resultSet.next();
			result = (resultSet.getInt(1) > 0);
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	protected static boolean existsEntry(String table, String column, int value){
		boolean result = false;
		try {
			PreparedStatement statement = Database.getPreparedStatement("SELECT COUNT(\""+column+"\") FROM \""+table+"\" WHERE \""+column+"\"=?;");
			statement.setInt(1, value);
			ResultSet resultSet = statement.executeQuery();
			resultSet.next();
			result = (resultSet.getInt(1) > 0);
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return result;
	}
	
	protected static void dropTable(String name,String properties){
		try {
			Statement statement = Database.getStatement();
			statement.execute("DROP TABLE \""+name+"\" "+properties+";");
			statement.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
